Android Architecture Components & Room Database - Topic 5 Summary

1. Introduction to Android Architecture Components (AAC)

AAC provides libraries to help developers build robust, testable, and maintainable Android apps following best architecture practices.
Why Use AAC?

Core Architecture Principles

2. ViewModel

ViewModel: Provides data for UI components and survives configuration changes (e.g., screen rotation).

Key Features:

Restaurant Analogy

Customer (UI) → Orders food
Server (ViewModel) → Takes order to kitchen
Chefs (Repository) → Prepare the meal
Kitchen (Data Sources) → Has ingredients and recipes

ViewModel Implementation

public class MyViewModel extends ViewModel { private MutableLiveData myData = new MutableLiveData<>(); public void loadData() { // Load data from repository myData.postValue("Data loaded"); } public LiveData getData() { return myData; } }
ViewModel Benefits:

3. Repository Pattern

Repository: Best practice pattern (not part of AAC libraries) that provides single, clean API to app data.

Key Responsibilities:

Multiple Data Sources

Repository can manage:

Repository Implementation

public class WordRepository { private WordDao wordDao; private LiveData> allWords; public WordRepository(Application application) { WordRoomDatabase db = WordRoomDatabase.getDatabase(application); wordDao = db.wordDao(); allWords = wordDao.getAllWords(); } public LiveData> getAllWords() { return allWords; } public void insert(Word word) { WordRoomDatabase.databaseWriteExecutor.execute(() -> { wordDao.insert(word); }); } }

4. Room Database

Room: Robust SQL object mapping library that simplifies SQLite database usage in Android apps.

Key Features:

Room Architecture

Room Components

Component Purpose Example
Entity Defines database table schema Java/Kotlin class with @Entity annotation
DAO Provides database access methods Interface with @Query, @Insert, @Update, @Delete annotations
Database Creates and manages database instance Abstract class extending RoomDatabase

4.1 Entity

Entity: Represents a database table row. Each instance = one row.

Key Annotations:

Entity Example

@Entity(tableName = "words") public class Word { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "word") private String word; @NonNull public String getWord() { return this.word; } // Getters and setters }

Entity Table Representation

Column Type Description
id INTEGER Primary key (auto-increment)
word TEXT User's word

4.2 DAO (Data Access Object)

DAO: Interface that defines database access methods using Room annotations.

Key Annotations:

DAO Example

@Dao public interface WordDao { @Insert void insert(Word word); @Update void update(Word word); @Delete void delete(Word word); @Query("DELETE FROM words") void deleteAll(); @Query("SELECT * FROM words ORDER BY word ASC") LiveData> getAllWords(); @Query("SELECT * FROM words WHERE word LIKE :word") List findWord(String word); }

4.3 Database

Database: Abstract class that serves as the main access point to your app's persisted data.

Key Features:

Database Example

@Database(entities = {Word.class}, version = 1) public abstract class WordRoomDatabase extends RoomDatabase { public abstract WordDao wordDao(); private static volatile WordRoomDatabase INSTANCE; private static final int NUMBER_OF_THREADS = 4; static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS); public static WordRoomDatabase getDatabase(final Context context) { if (INSTANCE == null) { synchronized (WordRoomDatabase.class) { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder( context.getApplicationContext(), WordRoomDatabase.class, "word_database" ).build(); } } } return INSTANCE; } }

5. Lifecycle-Aware Components

Lifecycle-Aware Components: Components that automatically adjust behavior based on lifecycle state of activity or fragment.

Key Benefits:

5.1 Lifecycle Library

5.2 Lifecycle Events and Observers

Event State Description
ON_CREATE Created Activity/fragment is created
ON_START Started Activity/fragment is visible
ON_RESUME Resumed Activity/fragment is in foreground
ON_PAUSE Paused Activity/fragment is partially obscured
ON_STOP Stopped Activity/fragment is no longer visible
ON_DESTROY Destroyed Activity/fragment is being destroyed

5.3 LiveData

LiveData: Observable data holder class that is lifecycle-aware and keeps data updated.

Key Features:

LiveData Implementation

// 1. Define in DAO @Query("SELECT * FROM words ORDER BY word ASC") LiveData> getAllWords(); // 2. Use in Repository public LiveData> getAllWords() { return wordDao.getAllWords(); } // 3. Use in ViewModel public LiveData> getAllWords() { return repository.getAllWords(); } // 4. Observe in Activity/Fragment wordViewModel.getAllWords().observe(this, words -> { // Update UI when words change adapter.setWords(words); });

LiveData Benefits

6. Complete Architecture Flow

User (UI Layer) → Requests data
ViewModel → Provides data and handles logic
Repository → Manages data sources
Room Database → Persists data locally
SQLite → Underlying database engine

LiveData → Observes changes and updates UI

7. Quick Reference Guide

Architecture Components Summary

Component Purpose Key Features
ViewModel Provide data to UI, survive configuration changes Clean UI separation, lifecycle awareness
Repository Single source of truth for data Data source management, clean API
Room Database SQLite abstraction layer Automatic code generation, compile-time checks
Entity Define database schema @Table annotation, column definitions
DAO Database access methods @Insert, @Update, @Delete, @Query annotations
LiveData Observable data with lifecycle awareness Automatic updates, configuration change handling

Room Annotations Cheat Sheet

Annotation Purpose Example
@Entity Mark class as database table @Entity(tableName = "words")
@PrimaryKey Define primary key @PrimaryKey(autoGenerate = true)
@ColumnInfo Customize column name @ColumnInfo(name = "word_text")
@NonNull Mark field as mandatory @NonNull private String word;
@Insert Insert operation @Insert void insert(Word word);
@Update Update operation @Update void update(Word word);
@Delete Delete operation @Delete void delete(Word word);
@Query Custom SQL query @Query("SELECT * FROM words")
@Database Define database class @Database(entities = {Word.class}, version = 1)

Best Practices

8. Exam Preparation Tips

9. Summary and Key Takeaways

Android Architecture Components (AAC) provide a modern, recommended approach to building Android apps with:

Key Benefits:

Architecture Flow: UI → ViewModel → Repository → Room Database → SQLite

Best Practice: Always follow the recommended app architecture for maintainable and scalable Android applications.